﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SnakeGame_Roman
{
    public class Snake
    {
        
        public int x { get; set; }
        public int y { get; set; }
        public int lenghtOfSnake { get; set; }
        public List<PieseOfSnake> piesesOfSnake { get; set; }
       
        public SnakeDirection direct { get; set; }
       
        private Snake()
        {

        }

        public Snake(int xHead, int yHead, int lenght)
            :this()
        {
            this.x = xHead;
            this.y = yHead;
            this.lenghtOfSnake = lenght;
            this.piesesOfSnake = new List<PieseOfSnake>();
           

            for (int i = 0; i < lenghtOfSnake; i++)
            {
                PieseOfSnake piese = new PieseOfSnake(x, y );
                piesesOfSnake.Add(piese);


            }


            
                          
         
        }

        public PieseOfSnake AddNewPiese(int x, int y)
        {
            PieseOfSnake newPiese = new PieseOfSnake(x,y);
            piesesOfSnake.Add(newPiese);
           return newPiese;

        }

        public void MoveSnake(Snake snake1)
        {
           
            if (Console.KeyAvailable == true)
            {
               
                var key = Console.ReadKey().Key;
                
                switch (key)
                {
                    case ConsoleKey.RightArrow:
                       
                       
                        if(direct == SnakeDirection.left) // запрет змее ползти в обратную сторону
                        {
                            snake1.piesesOfSnake[0].X = snake1.piesesOfSnake[0].X - 1;
                        }
                        else
                        {
                            snake1.piesesOfSnake[0].X = snake1.piesesOfSnake[0].X + 1;
                            this.direct = SnakeDirection.Right;
                        }
                        break;


                    case ConsoleKey.UpArrow:
                       
                        if (direct == SnakeDirection.Down)
                        {
                            snake1.piesesOfSnake[0].Y = snake1.piesesOfSnake[0].Y + 1;
                        }
                        else
                        {
                            snake1.piesesOfSnake[0].Y = snake1.piesesOfSnake[0].Y - 1;
                            this.direct = SnakeDirection.Up;
                        }
                        break;


                    case ConsoleKey.DownArrow:
                      
                       
                        if (direct == SnakeDirection.Up)
                        {
                            snake1.piesesOfSnake[0].Y = snake1.piesesOfSnake[0].Y - 1;
                        }
                        else
                        {
                            snake1.piesesOfSnake[0].Y = snake1.piesesOfSnake[0].Y + 1;
                            this.direct = SnakeDirection.Down;
                        }
                        break;


                    case ConsoleKey.LeftArrow:
                        
                        if (direct == SnakeDirection.Right)
                        {
                            snake1.piesesOfSnake[0].X = snake1.piesesOfSnake[0].X + 1;
                        }
                        else
                        {
                            snake1.piesesOfSnake[0].X = snake1.piesesOfSnake[0].X - 1;
                            this.direct = SnakeDirection.left;
                        }
                        break;
                }
            }

            else
            {
                switch (direct)
                {

                    case SnakeDirection.Right:
                        snake1.piesesOfSnake[0].X = snake1.piesesOfSnake[0].X + 1;
                        break;

                    case SnakeDirection.Up:
                        snake1.piesesOfSnake[0].Y = snake1.piesesOfSnake[0].Y - 1;
                        break;

                    case SnakeDirection.Down:
                        snake1.piesesOfSnake[0].Y = snake1.piesesOfSnake[0].Y + 1;
                        break;

                    case SnakeDirection.left:
                        snake1.piesesOfSnake[0].X = snake1.piesesOfSnake[0].X - 1;
                        break;
                }
            }
        }
      
        public enum SnakeDirection
        {
            Up,
            Down,
            Right,
            left,
            pause
        }
    }



  public class PieseOfSnake
    {
        public PieseOfSnake(int x, int y)
        {
            X = x;
            Y = y;
        }
        public int X { get; set; }
        public int Y { get; set; }
    }
}
